import * as React from 'react';
import { escape } from '@microsoft/sp-lodash-subset';
import { Link } from '@microsoft/office-ui-fabric-react-bundle';
import styles from './Home.module.scss';
import { SearchBox } from 'office-ui-fabric-react/lib/SearchBox';
import { PrimaryButton } from 'office-ui-fabric-react/lib/Button';
import { IIconProps } from 'office-ui-fabric-react';
export interface IProps {

}

var searchval = null;

const SearchFriendIcon: IIconProps = { iconName: 'Search' };

export default class Home extends React.Component<IProps, any> {

  // 构造函数
  constructor(props: any) {
    super(props);
    // 数据绑定
    this.state = {
      username: "",
      count: 0,
      sitelink: ""
    };
  }

  //动态更新绑定数据
  public async componentDidMount() {
    this.getTest();
  }

  private Searchbtn = (e) => {
    //
    this.getsearch(e);
  }

  public render(): React.ReactElement<IProps> {
    return (
      <div className={styles.layout_body_home}>
        <div className={styles.h_header}>
          {/* {this.state.username} */}
        </div>
        <div className={styles.h_logo}>
          <img src={require("../../../img/logo.jpg")}></img>
        </div>
        <div className={styles.h_title}>住化文控系统</div>
        <div className={styles.h_center_div}>
          <div className={styles.h_search_div}>
            <div className={styles.h_search}>
              <SearchBox
                className={styles.h_search_input}
                placeholder="Search"
                onClear={ev => {
                  searchval = null;
                }}
                onChange={(_, newValue) => {
                  searchval = newValue;
                }}
                onSearch={newValue => this.Searchbtn(newValue)}
              />
            </div>
            <div className={styles.h_search_btn}><PrimaryButton className={styles.h_search_btn_p} iconProps={SearchFriendIcon} text="搜索" onClick={() => this.Searchbtn(searchval)} allowDisabledFocus /></div>
            {/* <a id='searchtest' href="https://sumika.sharepoint.cn/sites/docmgr/DocLib/Forms/AllItems.aspx?view=7&q="></a> */}
          </div>
          <div className={styles.h_center_btn_div}>
            <div className={styles.h_center_btn}><a href="#/NewDocApply">文档新建申请</a></div>
            <div className={styles.h_center_btn}><a href="#/UpdateFile">文档变更申请</a></div>
            <div className={styles.h_center_btn}><a href="#/abandon">文档作废申请</a></div>
            <div className={styles.h_center_btn}><a href="https://sumika.sharepoint.cn/sites/docmgr/DocLib3/Forms/AllItems.aspx">PDF文档中心</a></div>
          </div>
        </div>
        <div className={styles.h_bottom_div}>
          <a href='#/apply'><div className={styles.h_bottom_btn1}>我的申请</div></a>
          <a href='#/task'><div className={styles.h_bottom_btn2}>我的待办</div></a>
        </div>
        <div>
        </div>

      </div>

    );
  }

  //访问list
  public async getTest() {
    var z: HTMLElement = document.querySelector("#header_div");
    //
    await this.setState({
      username: z.dataset.useremail,
      sitelink: z.dataset.sitelink
    });
  }

  //搜索
  public async getsearch(test) {
    //top.location.href = this.state.sitelink + "/_layouts/15/search.aspx/sitefiles?q=" + test;
    //location.href = "https://sumika.sharepoint.cn/sites/docmgr/DocLib/Forms/AllItems.aspx?view=7&q=" + test;
    // var a = document.createElement('a');
    //       a.style.display = 'none';
    //       a.href = "https://sumika.sharepoint.cn/sites/docmgr/DocLib/Forms/AllItems.aspx?view=7&q=" + test;
    //       document.body.appendChild(a);
    //       a.click();  // 自动触发点击a标签的click事件
    //       document.body.removeChild(a);
    if (test != null && test != "") {
      var a = document.createElement('a');
      a.style.display = 'none';
      a.href = "#/Search/" + test;
      document.body.appendChild(a);
      a.click();  // 自动触发点击a标签的click事件
      document.body.removeChild(a);
    }

    // var a:any=document.getElementById("searchtest");
    // a.href="https://sumika.sharepoint.cn/sites/docmgr/DocLib/Forms/AllItems.aspx?view=7&q=" + test;
    // a.click();
  }
}
